﻿using System;
using System.Web.UI.WebControls;
using VipCo.Data;
using VipCo.Entities;
using System.Data;

public partial class Diem_den : System.Web.UI.Page
{
    // Khai bao bien toan cuc
    public static int idTp_G = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["TP"] != null)
            {
                string sessionTp = Session["TP"].ToString();
                litSsTp.Text = sessionTp;
                HienthiTinhthanh(sessionTp);                
                HienthiChuyenMuc();
            }
            Pager1.CurrentIndex = 1;
            HienthiDiemden_DN(Pager1.CurrentIndex);
        }
    }

    /// <summary>
    /// Hien thi tinh thanh cua Thanh Pho hien tai
    /// </summary>
    /// <param name="tenTinh"></param>
    protected void HienthiTinhthanh(string tenTinh)
    {
        int totalRows = 0;
        string whereClause = "TenThanhPho like N'" + tenTinh + "' and TrangThai = 1";
        TList<ThanhPho> tlistThanhPhos = DataRepository.ThanhPhoProvider.GetPaged(
            whereClause, "Id DESC", 0, 0, out totalRows);

        if (tlistThanhPhos.Count > 0)
        {
            int idTp = tlistThanhPhos[0].Id;
            idTp_G = idTp;
            // Thuc hien lay danh sach Quan huyen cua Thanh pho do
            TList<QuanHuyen> tlistQuanHuyens = DataRepository.QuanHuyenProvider.GetByIdThanhPho(idTp);

            if (tlistQuanHuyens.Count > 0)
            {
                dlQuanhuyen.DataSource = tlistQuanHuyens;
                dlQuanhuyen.DataBind();
            }
        }
    }

    /// <summary>
    /// Hien thi cac diem den o Da nang
    /// </summary>
    protected void HienthiDiemden_DN(int pageNo)
    {
        int totalRows = 0;
        string whereClause = "IdThanhPho = " + idTp_G + " and GETDATE() BETWEEN NgayBatDau AND NgayKetThuc";
        DataSet dsDiemDen = DataRepository.Provider.ExecuteDataSet("DiaDiem_GetPaged", whereClause, "NgayKetThuc ASC", pageNo - 1, Pager1.PageSize);

        dlDiemden.DataSource = dsDiemDen.Tables[0];
        dlDiemden.DataBind();

        try
        {
            totalRows = Convert.ToInt32(dsDiemDen.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = totalRows;
        if (totalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    /// <summary>
    /// Hien thi danh sach cac chuyen muc
    /// </summary>
    protected void HienthiChuyenMuc()
    {
        int totalRows = 0;
        string whereClause = "TrangThai = 1";
        TList<NhomChuyenMuc> tlistChuyenMucs = DataRepository.NhomChuyenMucProvider.GetPaged(whereClause, "Id DESC", 0,
            0, out totalRows);
        if (tlistChuyenMucs.Count > 0)
        {
            dlDanhmuc.DataSource = tlistChuyenMucs;
            dlDanhmuc.DataBind();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="Char"></param>
    /// <returns></returns>
    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        HienthiDiemden_DN(currnetPageIndex);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="str"></param>
    /// <param name="n"></param>
    /// <returns></returns>
    public string formatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }

    public string KiemTraTrangThai(int trangthai)
    {
        if (trangthai == 1)
            return "<div class=\"xac-thuc\"><span class=\"icon-ok-circle icon-white\"></span><b> Đã được xác thực</b></div>";
        return "<div class=\"chua-xac-thuc\"><span class=\"icon-remove-circle icon-white\"></span><b> Chưa được xác thực</b></div>";
    }
}